package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.model.TaskAssignment;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.TaskAssignmentService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZQM
 * @date 2023/9/18 17:31
 */
@RestController
@RequestMapping("/v1/task")
public class TaskAssignmentController extends BaseController {

    @Autowired
    private TaskAssignmentService taskAssignmentService;

    /**
     * 添加任务
     */
    @PostMapping("/add")
    public JsonObj add(@RequestBody TaskAssignment taskAssignment) {
        return taskAssignmentService.add(taskAssignment);
    }

    /**
     * 删除任务
     */
    @GetMapping("/del")
    public JsonObj del(@RequestParam String id) {
        return taskAssignmentService.del(id);
    }

    /**
     * 修改任务
     */
    @PostMapping("/edit")
    public JsonObj edit(@RequestBody TaskAssignment taskAssignment) {
        return taskAssignmentService.edit(taskAssignment);
    }

    /**
     * 查询任务（单个）
     */
    @GetMapping("/selById")
    public JsonObj selById(@RequestParam String id) {
        Map<String, Object> assignment = taskAssignmentService.selById(id);
        return JsonObj.success(assignment);
    }

    /**
     * 查询任务（列表）
     */
    @PostMapping("/selList")
    public ListResult<Object> selList(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(taskAssignmentService.selList(pageData));
    }

    /**
     * 封库（锁定）
     */
    @GetMapping("/changeStatus")
    public JsonObj changeStatus(@RequestParam String id,@RequestParam String status) {
        return taskAssignmentService.changeStatus(id,status);
    }

    /**
     * 导出
     */
    @PostMapping("/queryAllExportData")
    public JsonObj queryAllExportData(@RequestBody PageData pageData) {
        List<LinkedHashMap<String, Object>> list = taskAssignmentService.queryAllExportDataBy(pageData);
        Map<String, Object> map = new HashedMap<>();
        map.put("data", list);
        return JsonObj.success(map);
    }

    @PostMapping(value = "/importGJData")
    @ApiOperation("导入（国家格式）")
    public JsonObj importGJData(@RequestParam("file") MultipartFile file, HttpServletResponse response)
            throws IOException {
        PageData pageData = getPageData();
        String userId = (String) pageData.get("user");
        return taskAssignmentService.importGJData(file,response,userId);
    }


}
